﻿/*+===================================================================
File:      ScoreState.cpp

Summary:   Hiện thực hóa các phương thức của lớp CScoreState
===================================================================+*/

#include "ScoreState.h"

CScoreState::CScoreState()
{
	m_bTwinkle = true;
	Init();
}

CScoreState::~CScoreState()
{
	if (m_pPlayer1)
		delete m_pPlayer1;
}

void CScoreState::Init()
{
	m_pPlayer1 = CResourcesManager::GetInstance()->GetTexture(ID_TEXTURE_PLAYER1_TEXTURE);
	m_bTwinkle = false;
	m_fTwinkleTime = 0.0f;
	m_fTimeEnd = 2.0f;
}

void CScoreState::Update(float deltaTime)
{
	if (m_bFinished)
		return;
	if (m_fTwinkleTime > 0)
		m_fTwinkleTime -= deltaTime;
	else
	{
		m_fTwinkleTime = 0.2f;
		m_bTwinkle = !m_bTwinkle;
	}
	if (m_fTimeEnd > 0)
		m_fTimeEnd -= deltaTime;
	else
	{
		this->End();
	}
}

void CScoreState::Render()
{
	if (m_bFinished)
		return;
	LPD3DXFONT* ppFont = CGraphic::GetInstance()->GetFontHandle();
	LPD3DXSPRITE ppSprite = CGraphic::GetInstance()->GetSpriteHandler();

	char info[50];
	RECT destSource;

	sprintf_s(info, "1P    : %d", g_nScore);
	destSource.left = 28;
	destSource.top = 64;
	destSource.bottom = 79;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	sprintf_s(info, "REST : %d", g_nLives);
	destSource.left = 28;
	destSource.top = 95;
	destSource.bottom = 110;
	destSource.right = 245;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	int hiScore;

	switch (g_nCurentLvl)
	{
	case ID_LEVEL1:
		hiScore = MAX_SCORE_LEVEL_1;
		break;
	case ID_LEVEL2:
		hiScore = MAX_SCORE_LEVEL_2;
		break;
	case ID_LEVEL3:
		hiScore = MAX_SCORE_LEVEL_3;
		break;
	}

	if (g_nScore > hiScore)
		hiScore = g_nScore;

	sprintf_s(info, "HI : %d", hiScore);
	destSource.left = 170;
	destSource.top = 145;
	destSource.bottom = 160;
	destSource.right = 500;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	sprintf_s(info, "STAGE %d", g_nCurentLvl);
	destSource.left = 210;
	destSource.top = 255;
	destSource.bottom = 270;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_LEFT, 0xFFFFFFFF);

	char levelName[35];
	switch (g_nCurentLvl)
	{
	case ID_LEVEL1:
		strcpy_s(levelName, "JUNGLE");
		break;
	case ID_LEVEL2:
		strcpy_s(levelName, "WATERFALL");
		hiScore = MAX_SCORE_LEVEL_2;
		break;
	case ID_LEVEL3:
		strcpy_s(levelName, "SNOW FIELD");
		hiScore = MAX_SCORE_LEVEL_3;
		break;
	}

	sprintf_s(info, "%s", levelName);
	destSource.left = 150;
	destSource.top = 288;
	destSource.bottom = 303;
	destSource.right = 400;

	(*ppFont)->DrawTextA(ppSprite, info, strlen(info), &destSource, DT_CENTER, 0xFFFFFFFF);

	if (m_bTwinkle)
	{
		m_pPlayer1->Draw(D3DXVECTOR2(235, 63));
		m_pPlayer1->Draw(D3DXVECTOR2(335, 145));
	}
}

void CScoreState::End()
{
	m_bFinished = true;
	delete m_pNextState;
	m_pNextState = new CPlayState();
	CGameStateManager::GetInstance()->ChangeState(m_pNextState);	
}